package com.jinlejia.erp.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.databinding.ItemRentLayoutBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * 选择租赁Adapter
 */
public class SelectRentAdapter extends YfListAdapter<Map<String, Object>> {

    private Context context;
    private String showBtn;
//    private HashMap<Integer, Boolean> selectMap = new HashMap<>();
    private HashSet<String> IDs = new HashSet<>();

    public SelectRentAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    public SelectRentAdapter(ArrayList<Map<String, Object>> data, String showBtn) {
        super(data);
        this.showBtn = showBtn;
    }

    public void setSelectMap(HashMap<Integer, Boolean> map) {
//        this.selectMap = map;
    }

    public void setSelectHouseID( HashSet<String> IDs ){
        this.IDs = IDs;
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rent_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        final ItemRentLayoutBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        Map<String, Object> map = mData.get(i);
        if( IDs.contains(map.get("rscode")) ) {
            if(dListiner != null)
                dListiner.onDefault(map);
        }
        if (map.containsKey("image"))
            Glide.with(context).load(ApiConfig.MAGEHEADER_URL() + map.get("image")).placeholder(R.mipmap.ic_no_img).error(R.mipmap.ic_no_img).into(binding.ivHouse);
        //小区名
        if (map.containsKey("estate"))
            binding.tvHouseName.setText(map.get("estate").toString());
        //商圈
        if (map.containsKey("area")){
            if (map.containsKey("county")){
                binding.tvHouseDistrict.setText(map.get("county").toString()+" - "+map.get("area").toString());
            }else {
                binding.tvHouseDistrict.setText(map.get("area").toString());
            }
        }
        //户型
        if (map.containsKey("houseType")){
            binding.tvHouseType.setText(map.get("houseType").toString());
            if (TextUtils.isEmpty(mData.get(i).get("houseType").toString())){
                binding.tvHouseType.setVisibility(View.GONE);
            }else {
                binding.tvHouseType.setVisibility(View.VISIBLE);
            }
        }

        //面积
        if (map.containsKey("square"))
            binding.tvHouseArea.setText(map.get("square") + "㎡");
        //租金
        if (map.containsKey("rentPrice"))
            binding.tvHouseTotle.setText(map.get("rentPrice") + "元/月");

        if (!TextUtils.isEmpty(showBtn) && showBtn.equals("DEL")) {
            binding.btDelete.setVisibility(View.VISIBLE);
        } else if (!TextUtils.isEmpty(showBtn) && showBtn.equals("CHECK")) {
            binding.checkbox.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.isEmpty(showBtn) && showBtn.equals("CHECK")) {
            binding.checkbox.setVisibility(View.VISIBLE);
            if (IDs.contains(map.get("rscode"))) {
                binding.checkbox.setChecked(true);
            } else {
                binding.checkbox.setChecked(false);
            }

        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (IDs.contains(mData.get(i).get("rscode"))) {
                    binding.checkbox.setChecked(false);
                    IDs.remove(mData.get(i).get("rscode"));
                    if( null!= rListiner ){
                        rListiner.onRemove( mData.get(i));
                    }
                } else {
                    binding.checkbox.setChecked(true);
                    IDs.add(mData.get(i).get("rscode").toString());
                    if (listiner != null) {
                        listiner.Onselected(mData.get(i));
                    }
                }

            }
        });

        binding.getRoot().setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }


    static final class ViewHolder extends RecyclerView.ViewHolder {


        public ViewHolder(View itemView) {
            super(itemView);
            ItemRentLayoutBinding binding = DataBindingUtil.bind(itemView);
            binding.checkbox.setVisibility(View.VISIBLE);
        }
    }

    public interface OnSelectedListiner {
        void Onselected(Map<String, Object> map);
    }

    private OnSelectedListiner listiner;

    public void setOnSelectedListiner(OnSelectedListiner l) {
        this.listiner = l;
    }

    public interface OnRemoveListiner {
        void onRemove(Map<String, Object> map);
    }

    private OnRemoveListiner rListiner;

    public void setOnRemoveListiner(OnRemoveListiner l) {
        this.rListiner = l;
    }

    public interface OnDefaultSelectListiner {
        void onDefault(Map<String, Object> map);
    }

    private OnDefaultSelectListiner dListiner;

    public void setOnDefaultListiner(OnDefaultSelectListiner l) {
        this.dListiner = l;
    }

}
